//
//  BDMyCouponsCell.m
//  iOS_beboy
//
//  Created by wen on 2017/9/21.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyCouponsCell.h"

@implementation BDMyCouponsTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    
    _titleLabel = [YYLabel new];
    _titleLabel.clipsToBounds = YES;
    _titleLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _titleLabel.displaysAsynchronously = YES;
    _titleLabel.ignoreCommonProperties = YES;
    _titleLabel.fadeOnAsynchronouslyDisplay = NO;
    _titleLabel.fadeOnHighlight = NO;
    [self addSubview:_titleLabel];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [self addSubview:_line];
    
    return self;
}

- (void)setModel:(BDMyCouponsModel *)model{
    self.height = 35;
    
    NSMutableAttributedString *text = [NSMutableAttributedString new];
    NSMutableAttributedString *subString = [NSMutableAttributedString attachmentStringWithEmojiImage:[UIImage imageNamed:@"dianpu"] fontSize:15];
    [text appendAttributedString:subString];
    NSString *shopName = model.shopTitle;

    if ([shopName isNotBlank] == NO) {
        shopName = ASLocalizedString(@"该店不存在");
    }
    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@", shopName]];
    titleString.font = [UIFont systemFontOfSize:15];
    titleString.color = [UIColor colorWithHexString:@"#111111"];
    [text appendAttributedString:titleString];
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kBDMyCouponsCellContentTextWidth, CGFLOAT_MAX);
    container.maximumNumberOfRows = 1;
    
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:container text:text];
    
    _titleLabel.size = titleLayout.textBoundingSize;
    _titleLabel.textLayout = titleLayout;
    _titleLabel.left = 10;
    _titleLabel.centerY = self.height / 2.0;
    
    _line.width = self.width;
    _line.height = 1;
    _line.bottom = self.height;
}

@end

@implementation BDMyCouponsToolbar

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [self addSubview:_line];
    
    _toolLabel = [YYLabel new];
    _toolLabel.clipsToBounds = YES;
    _toolLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _toolLabel.displaysAsynchronously = YES;
    _toolLabel.ignoreCommonProperties = YES;
    _toolLabel.fadeOnAsynchronouslyDisplay = NO;
    _toolLabel.fadeOnHighlight = NO;
    [self addSubview:_toolLabel];
    
    return self;
}

- (void)setModel:(BDMyCouponsModel *)model{
    self.height = 35;
    
    _line.width = self.width - 20;
    [_line drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#e5e5e5"] verticality:NO];
    _line.height = 1;
    _line.left = 10;
    _line.top = 0;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-mm-dd HH:mm:ss"];
    
    NSDate *start_at = [NSDate dateWithTimeIntervalSince1970:model.start_at];
    NSString *startString = [formatter stringFromDate:start_at];
    
    NSDate *end_at = [NSDate dateWithTimeIntervalSince1970:model.end_at];
    NSString *endString = [formatter stringFromDate:end_at];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), startString, endString]];
    text.font = [UIFont systemFontOfSize:13];
    text.color = [UIColor colorWithHexString:@"#767676"];
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kBDScreenWidth - 20, CGFLOAT_MAX);
    container.maximumNumberOfRows = 1;
    
    YYTextLayout *toolLayout = [YYTextLayout layoutWithContainer:container text:text];
    
    _toolLabel.size = toolLayout.textBoundingSize;
    _toolLabel.textLayout = toolLayout;
    _toolLabel.left = 10;
    _toolLabel.centerY = self.height / 2.0;
}

@end

@implementation BDMyCouponsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
//    self.exclusiveTouch = YES;
    
    _contentView = [UIView new];
    _contentView.width = kScreenWidth;
    _contentView.height = 1;
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.clipsToBounds = YES;
    static UIImage *topLineBG, *bottomLineBG;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        topLineBG = [UIImage imageWithSize:CGSizeMake(1, 3) drawBlock:^(CGContextRef context) {
            CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
            CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 0.8, [UIColor colorWithWhite:0 alpha:0.08].CGColor);
            CGContextAddRect(context, CGRectMake(-2, 3, 4, 4));//colorWithWhite:0 alpha:0.08
            CGContextFillPath(context);
        }];
        bottomLineBG = [UIImage imageWithSize:CGSizeMake(1, 3) drawBlock:^(CGContextRef context) {
            CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
            CGContextSetShadowWithColor(context, CGSizeMake(0, 0.4), 2, [UIColor colorWithWhite:0 alpha:0.08].CGColor);
            CGContextAddRect(context, CGRectMake(-2, -2, 4, 2));
            CGContextFillPath(context);
        }];
    });
    UIImageView *topLine = [[UIImageView alloc] initWithImage:topLineBG];
    topLine.width = _contentView.width;
    topLine.bottom = 0;
    topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    [_contentView addSubview:topLine];
    
    
    UIImageView *bottomLine = [[UIImageView alloc] initWithImage:bottomLineBG];
    bottomLine.width = _contentView.width;
    bottomLine.top = _contentView.height;
    bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_contentView addSubview:bottomLine];
    
    [self addSubview:_contentView];
    
    _titleView = [BDMyCouponsTitleView new];
    [_contentView insertSubview:_titleView atIndex:0];
    
    __weak BDMyCouponsView *weak_self = self;
    _userButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _userButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _userButton.size = CGSizeMake(kBDMyCouponsCellContentButtonWidth, 30);
    _userButton.layer.masksToBounds = YES;
    _userButton.layer.cornerRadius = 5;
    [_userButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [_userButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if ([weak_self.cell.delegate respondsToSelector:@selector(cellDidClickButton:)]) {
            [weak_self.cell.delegate cellDidClickButton:weak_self.cell];
        }
    }];
    [_userButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [_contentView addSubview:_userButton];
    
    _contentImageView = [[UIImageView alloc] init];
    [_contentView addSubview:_contentImageView];
    _contentImageView.size = CGSizeMake(kBDMyCouponsCellContentImageWidth, kBDMyCouponsCellContentImageHeight);
    _contentImageView.layer.masksToBounds = YES;
    _contentImageView.layer.cornerRadius = 5;
    
    _contentLabel = [YYLabel new];
    _contentLabel.clipsToBounds = YES;
    _contentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _contentLabel.displaysAsynchronously = YES;
    _contentLabel.ignoreCommonProperties = YES;
    _contentLabel.fadeOnAsynchronouslyDisplay = NO;
    _contentLabel.fadeOnHighlight = NO;
    [_contentView addSubview:_contentLabel];
    
    _priceLabel = [YYLabel new];
    _priceLabel.clipsToBounds = YES;
    _priceLabel.left = _contentImageView.right + kBDMyCouponsCellContentTextPadding;
    _priceLabel.width = kBDMyCouponsCellContentTextWidth;
    _priceLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _priceLabel.displaysAsynchronously = YES;
    _priceLabel.ignoreCommonProperties = YES;
    _priceLabel.fadeOnAsynchronouslyDisplay = NO;
    _priceLabel.fadeOnHighlight = NO;
    [_contentView addSubview:_priceLabel];
    
    _toolbarView = [BDMyCouponsToolbar new];
    [_contentView addSubview:_toolbarView];
    
    return self;
}

- (void)setModel:(BDMyCouponsModel *)model {
    _model = model;
    self.height = kBDMyCouponsCellHeight;
    
    _contentView.top = 8;
    _contentView.height = self.height - 10;
    
    [_titleView setModel:model];
    
    CGFloat top = _titleView.bottom;
    _contentImageView.left = 10;
    _contentImageView.top = kBDMyCouponsCellContentImagePadding + top;
    [_contentImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"shop_pic"]];
    
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:model.title];
    titleText.font = [UIFont systemFontOfSize:15];
    titleText.color = [UIColor colorWithHexString:@"#111111"];
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kBDMyCouponsCellContentTextWidth, CGFLOAT_MAX);
    container.maximumNumberOfRows = 1;
    YYTextLayout *contentLayout = [YYTextLayout layoutWithContainer:container text:titleText];
    
    _contentLabel.size = contentLayout.textBoundingSize;
    _contentLabel.textLayout = contentLayout;
    _contentLabel.left = _contentImageView.right + kBDMyCouponsCellContentTextPadding;
    _contentLabel.top = _contentImageView.top + 5;
    
    NSMutableAttributedString *priceText = [NSMutableAttributedString new];
    NSMutableAttributedString *renminbi = [[NSMutableAttributedString alloc] initWithString:@"¥ "];
    renminbi.color = [UIColor colorWithHexString:@"#ff4f00"];
    renminbi.font = [UIFont systemFontOfSize:12];
    [priceText appendAttributedString:renminbi];
    
    NSMutableAttributedString *moneyText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [NSDecimalNumber decimalNumberWithString:model.amount]]];
    moneyText.font = [UIFont systemFontOfSize:16];
    moneyText.color = [UIColor colorWithHexString:@"#ff4f00"];
    [priceText appendAttributedString:moneyText];
    
    YYTextContainer *moneyContainer = [YYTextContainer new];
    moneyContainer.size = CGSizeMake(kBDMyCouponsCellContentTextWidth, CGFLOAT_MAX);
    moneyContainer.maximumNumberOfRows = 1;
    YYTextLayout *priceLayout = [YYTextLayout layoutWithContainer:container text:priceText];
    
    _priceLabel.size = priceLayout.textBoundingSize;
    _priceLabel.textLayout = priceLayout;
    _priceLabel.left = _contentImageView.right + kBDMyCouponsCellContentTextPadding;
    _priceLabel.bottom = _contentImageView.bottom - 5;
    
    switch (model.status) {
        case BDMyCouponsStatusNotUse:
            [_userButton setTitle:ASLocalizedString(@"使用")forState:UIControlStateNormal];
            [_userButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _userButton.backgroundColor = KBDColorBlue;
            _userButton.userInteractionEnabled = YES;
            break;
        case BDMyCouponsStatusUsed:
            [_userButton setTitle:ASLocalizedString(@"评价")forState:UIControlStateNormal];
            [_userButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _userButton.backgroundColor = KBDColorBlue;
            _userButton.userInteractionEnabled = YES;
            break;
        case BDMyCouponsStatusOverdue:
            [_userButton setTitle:ASLocalizedString(@"已过期")forState:UIControlStateNormal];
            [_userButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
            _userButton.backgroundColor = [UIColor whiteColor];
            _userButton.userInteractionEnabled = NO;
            break;
        case BDMyCouponsStatusRefunding:
            [_userButton setTitle:ASLocalizedString(@"退款中")forState:UIControlStateNormal];
            [_userButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
            _userButton.backgroundColor = [UIColor whiteColor];
            _userButton.userInteractionEnabled = NO;
            break;
        case BDMyCouponsStatusRefunded:
            [_userButton setTitle:ASLocalizedString(@"已退款")forState:UIControlStateNormal];
            [_userButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
            _userButton.backgroundColor = [UIColor whiteColor];
            _userButton.userInteractionEnabled = NO;
            break;
        case BDMyCouponsStatusCommented:
            [_userButton setTitle:ASLocalizedString(@"已完成")forState:UIControlStateNormal];
            [_userButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
            _userButton.backgroundColor = [UIColor whiteColor];
            _userButton.userInteractionEnabled = NO;
            break;
        default:
            break;
    }

    _userButton.right = self.width - 10;
    _userButton.centerY = _contentImageView.centerY;
    
    top = _contentImageView.bottom + kBDMyCouponsCellContentImagePadding;
    
    [_toolbarView setModel:model];
    _toolbarView.top = top;
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [(_contentView) performSelector:@selector(setBackgroundColor:) withObject:kBDMyCouponsCellHighlightColor afterDelay:0.15];
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesRestoreBackgroundColor];
    if ([_cell.delegate respondsToSelector:@selector(cellDidClick:)]) {
        [_cell.delegate cellDidClick:_cell];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesRestoreBackgroundColor];
}

- (void)touchesRestoreBackgroundColor {
    [NSObject cancelPreviousPerformRequestsWithTarget:_contentView selector:@selector(setBackgroundColor:) object:kBDMyCouponsCellHighlightColor];
    _contentView.backgroundColor = [UIColor whiteColor];
}

@end

@implementation BDMyCouponsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    _couponsView = [BDMyCouponsView new];
    [self.contentView addSubview:_couponsView];
    _couponsView.cell = self;
    self.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    return self;
}

- (void)setModel:(BDMyCouponsModel *)model{
    self.height = kBDMyCouponsCellHeight;
    self.contentView.height = self.height;
    [_couponsView setModel:model];
}

@end
